package riteh.eventNetwork.client;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.view.View.OnClickListener;

public class ActivityCircleEdit extends Activity {
	
	private SeekBar seekRed;
	private SeekBar seekGreen;
	private SeekBar seekBlue;
	private Button btnSave;
	private EditText editName;
	private TextView labelColor;
	private String oldName;
	private int r,g,b;
	private long userId, circleId;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(C.TAG, "CIRCLE_EDIT Activity");
		setContentView(R.layout.circle_edit);
		
	// INSTANCIRANJE OBJEKTA
		
		seekRed = (SeekBar) findViewById(R.id.circleEditSeekRed);
		seekGreen = (SeekBar) findViewById(R.id.circleEditSeekGreen);
		seekBlue = (SeekBar) findViewById(R.id.circleEditSeekBlue);
		btnSave = (Button) findViewById(R.id.circleEditButtonSave);
		editName = (EditText) findViewById(R.id.circleEditEditName);
		labelColor = (TextView) findViewById(R.id.circleEditLabelColor);
		
	// AKCIJE NA STARTU
		r = g = b = 0;
		userId = getIntent().getExtras().getLong("userId");
		circleId = getIntent().getExtras().getLong("circleId");
		
		if (circleId > 0) {
			String color = getIntent().getExtras().getString("circleColor");
			r = Color.red(Color.parseColor(color));
			g = Color.green(Color.parseColor(color));
			b = Color.blue(Color.parseColor(color));
			oldName = getIntent().getStringExtra("circleName");
			editName.setText(oldName);
			
			seekRed.setProgress((int)(r/2.55));
			seekGreen.setProgress((int)(g/2.55));
			seekBlue.setProgress((int)(b/2.55));
			labelColor.setTextColor(Color.rgb(r,g,b));
		}
		
		// Postavi rezultat za slucaj da se klikne back 
		setResult(C.ERROR);
		
	// EVENT LISTENERS	
		
		seekRed.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {}
			
			public void onStartTrackingTouch(SeekBar seekBar) {	}
			
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				r = (int) (progress * 2.55);
				labelColor.setTextColor(Color.rgb(r, g, b));
			}
		});
		
		seekGreen.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {}
			
			public void onStartTrackingTouch(SeekBar seekBar) {	}
			
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				g = (int) (progress * 2.55);
				labelColor.setTextColor(Color.rgb(r, g, b));
			}
		});
		
		seekBlue.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {}
			
			public void onStartTrackingTouch(SeekBar seekBar) {	}
			
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				b = (int) (progress * 2.55);
				labelColor.setTextColor(Color.rgb(r, g, b));
			}
		});
		
		btnSave.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				if (userId == C.TEST_ID) return;
				
				if (circleId == 0) {
					createCircle(editName.getText().toString());
				}
				else {
					editCircle(editName.getText().toString());
				}
			}
		});
	}
	
	private void createCircle(String name) {
		Log.d(C.TAG,"CIRCLE_EDIT: createCircle()");
		
		RestClient client = new RestClient(getString(R.string.serverMethodCreateCircle));
		client.AddParam("userId", String.valueOf(userId));
		client.AddParam("circleName", name);
		client.AddParam("circleColor", String.format("#%X", Color.rgb(r, g, b)));
		
		String result = C.getMethod0(client, "createCircle");
		
		if (C.resultSuccess(this, result, getString(R.string.toastCircleEditError))) {
			setResult(C.SUCCESS);
			finish();
		}

	}
	
	private void editCircle(String newName) {
		Log.d(C.TAG,"CIRCLE_EDIT: editCircle()");
		
		RestClient client = new RestClient(getString(R.string.serverMethodEditCircle));
		client.AddParam("userId", String.valueOf(userId));
		client.AddParam("circleId", String.valueOf(circleId));
		client.AddParam("circleName", newName);
		client.AddParam("circleColor", String.format("#%X", Color.rgb(r, g, b)));
		
		String result = C.getMethod0(client,getString(R.string.serverMethodEditCircle));
		
		if (C.resultSuccess(this, result, getString(R.string.toastCircleEditError))) {
			setResult(C.SUCCESS);
			finish();
		}

	}
}
